local M = {
	dragRect = nil
}

function M.createRect(proxyObj)
	local gRect = display.newGroup()

	local rect = display.newRect(0, 0, 330, 24)
	rect:setFillColor(unpack(colorCfg[3]))
	rect.anchorX = 0
	rect.anchorY = 0
	rect:setStrokeColor(unpack(colorCfg[5]))
	rect.strokeWidth = 1
	gRect:insert(rect)

	local rectA = display.newRect(0, 0, 330, 6)
	rectA:setFillColor(0, 0, 1)
	rectA.anchorX = 0
	rectA.anchorY = 0
	rectA.alpha = 0
	gRect:insert(rectA)

	local rectB = display.newRect(0, 6, 330, 12)
	rectB:setFillColor(1, 1, 1)
	rectB.anchorX = 0
	rectB.anchorY = 0
	rectB.alpha = 0.1
	gRect:insert(rectB)

	local rectC = display.newRect(0, 18, 330, 6)
	rectC:setFillColor(1, 0, 0)
	rectC.anchorX = 0
	rectC.anchorY = 0
	rectC.alpha = 0
	gRect:insert(rectC)

	local function onMid(event)
		if not M.dragRect then
			M.dragRect = display.newRect(0, 0, 100, 20)
			M.dragRect.alpha = 0.3
		end

		M.dragRect.x = event.x
		M.dragRect.y = event.y
	end

	local function onCancelMid(event)
		if M.dragRect then
			M.dragRect:removeSelf()
			M.dragRect = nil
		end

		eventDispatcher:dispatchEvent("hierDragRelease", { target = proxyObj, itemObj = gRect, x = event.x, y = event.y })
	end

	inputUtils.mouseDelegate({
		target = gRect,
		onMid = onMid,
		onCancelMid = onCancelMid,
	})

	-- 点击后，会派发消息通知点击，
	local function onTap(event)
		eventDispatcher:dispatchEvent("clickHieObj", { target = proxyObj })
		eventDispatcher:dispatchEvent("chooseObj", { target = proxyObj })
	end
	rect:addEventListener("tap", onTap)

	local function onReleaseDrag(event)
		-- 和自己无关
		if not uiUtils.isInBounds(gRect, event.x, event.y) then
			return
		end

		-- 依旧在自己的位置
		if event.target == proxyObj then
			print("不能把自己拖动到自己")
			return
		end

		-- 删除自己所在项
		--local function updateOrigParent()
		--	eventDispatcher:dispatchEvent("changeParentDelete", {target = event.target})
		--end

		-- 更新自己新的父组
		local function updateNowParent(origParent, nowParent)
			-- 定位到gItem
			eventDispatcher:dispatchEvent("changeParentDelete", {target = origParent})
			eventDispatcher:dispatchEvent("changeParentAdd", {target = nowParent})
		end

		-- 同组，显示在自己后面
		if uiUtils.isInBounds(rectA, event.x, event.y) then
			local localIndex = 0
			for i = 1, proxyObj.parent.numChildren do
				if proxyObj.parent[i] == proxyObj then
					localIndex = i
					break
				end
			end
			local origParent = event.target.parent
			proxyObj.parent:insert(localIndex, event.target)
			updateNowParent(origParent, event.target.parent)
			-- 筛入自己组
		elseif uiUtils.isInBounds(rectB, event.x, event.y) then
			if not coreDisplay.isGroup(proxyObj) then
				print("对象不是组，无法筛入")
				return
			end
			local origParent = event.target.parent
			proxyObj:insert(event.target)
			updateNowParent(origParent, event.target.parent)
			--同组，显示在自己前面（）
		elseif uiUtils.isInBounds(rectC, event.x, event.y) then
			local localIndex = 0
			for i = 1, proxyObj.parent.numChildren do
				if proxyObj.parent[i] == proxyObj then
					localIndex = i
					break
				end
			end
			local origParent = event.target.parent
			-- 真实的筛组
			proxyObj.parent:insert(localIndex + 1, event.target)
			updateNowParent(origParent, event.target.parent)
		end
	end

	-- 监听选中事件
	local function onEvent(event)
		if event.target == proxyObj then
			rect:setFillColor(unpack(colorCfg[5]))
		else
			rect:setFillColor(unpack(colorCfg[3]))
		end
	end

	local function onReleaseComp(event)
		-- 和自己无关
		if not uiUtils.isInBounds(gRect, event.x, event.y) then
			return
		end

		-- 依旧在自己的位置
		if event.target == proxyObj then
			print("不能把自己拖动到自己")
			return
		end

		-- 删除自己所在项
		--local function updateOrigParent()
		--	eventDispatcher:dispatchEvent("changeParentDelete", {target = event.target})
		--end

		-- 更新自己新的父组
		local function updateNowParent(origParent, nowParent)
			-- 定位到gItem
			eventDispatcher:dispatchEvent("changeParentDelete", {target = origParent})
			eventDispatcher:dispatchEvent("changeParentAdd", {target = nowParent})
		end

		-- 同组，显示在自己后面
		if uiUtils.isInBounds(rectA, event.x, event.y) then
			local localIndex = 0
			for i = 1, proxyObj.parent.numChildren do
				if proxyObj.parent[i] == proxyObj then
					localIndex = i
					break
				end
			end
			local origParent = event.target.parent
			proxyObj.parent:insert(localIndex, event.target)
			updateNowParent(origParent, event.target.parent)
			-- 筛入自己组
		elseif uiUtils.isInBounds(rectB, event.x, event.y) then
			if not coreDisplay.isGroup(proxyObj) then
				print("对象不是组，无法筛入")
				return
			end
			local origParent = event.target.parent
			proxyObj:insert(event.target)
			updateNowParent(origParent, event.target.parent)
			--同组，显示在自己前面（）
		elseif uiUtils.isInBounds(rectC, event.x, event.y) then
			local localIndex = 0
			for i = 1, proxyObj.parent.numChildren do
				if proxyObj.parent[i] == proxyObj then
					localIndex = i
					break
				end
			end
			local origParent = event.target.parent
			-- 真实的筛组
			proxyObj.parent:insert(localIndex + 1, event.target)
			updateNowParent(origParent, event.target.parent)
		end
	end

	eventDispatcher:addEventListener("hierDragRelease", onReleaseDrag, function()
		return not coreDisplay.isDisplayObj(gRect)
	end)

	eventDispatcher:addEventListener(
			"chooseObj",
			onEvent,
			function()
				return not coreDisplay.isDisplayObj(gRect)
			end
	)
	eventDispatcher:addEventListener(
			"clickHieObj",
			onEvent,
			function()
				return not coreDisplay.isDisplayObj(gRect)
			end
	)
	eventDispatcher:addEventListener(
			"compRelease",
			onReleaseComp,
			function()
				return not coreDisplay.isDisplayObj(gRect)
			end
	)

	return gRect
end

return M